import { RandomManager } from "db://oops-framework/core/common/random/RandomManager";
import { smc } from "../../../../common/ecs/SingletonModuleComp";
import { RoleAttributeType } from "../../../../role/model/RoleEnum";
import { Role } from "../../../../role/Role";
import { BattleType } from "../../../scene/SceneEvent";
import { RoleState } from "./RoleState";

/** 角色选择器 */
export class RoleSelector {
    /** 选中相同阵营的角色 */
    static friend(battleType: BattleType): Role[] {
        var friends: Role[] = [];
        switch (battleType) {
            case BattleType.Scene:
                friends = smc.scene.MapModelRole.friends;
                break;
            case BattleType.Boss:
                // friends = smc.boss.BossMapModelRole.friends;
                console.warn("角色选择器：boss todo");
                break;
            case BattleType.Tower:
                // friends = smc.tower.TowerMapModelRole.friends;
                console.warn("角色选择器：塔 todo");

                break;

            default:
                break;
        }
        return friends;
    }
    /** 选中所有敌对角色 */
    static enemy(battleType: BattleType): Role[] {
        var enemys: Role[] = [];
        switch (battleType) {
            case BattleType.Scene:
                enemys = smc.scene.MapModelRole.enemys;
                break;
            case BattleType.Boss:
                // enemys = smc.boss.BossMapModelRole.enemys;
                console.warn("角色选择器：boss todo");
                break;
            case BattleType.Tower:
                // enemys = smc.tower.TowerMapModelRole.enemys;
                console.warn("角色选择器：塔 todo");

                break;

            default:
                break;
        }
        return enemys;
    }

    /** 获取友方上阵活着武将列表 */
    static youfangRoles(role: Role): Role[] {
        var list: Role[] = [];
        var allList: Role[] = [];
        if (role.RoleModel.myTeam) {
            allList = RoleSelector.friend(role.RoleModel.battleType);
        } else {
            allList = RoleSelector.enemy(role.RoleModel.battleType);
        }

        allList.forEach(role => {
            if (!RoleState.isDie(role)) {
                list.push(role);
            }
        });

        return list;
    }

      /** 获取敌方上阵活着武将列表 */
      static difangRoles(role: Role): Role[] {
        var list: Role[] = [];
        var allList: Role[] = [];
        if (role.RoleModel.myTeam) {
            allList = RoleSelector.enemy(role.RoleModel.battleType);
        } else {
            allList = RoleSelector.friend(role.RoleModel.battleType);
        }

        allList.forEach(role => {
            if (!RoleState.isDie(role)) {
                list.push(role);
            }
        });

        return list;
    }

     /**
     * 选择攻击目标
     * @param type 攻击类型
     
     *  1:  默认攻击
        2:  前排
        3:  后排
        4:  随机3个
        5:  全体
        6:  攻击力最高
        7:  攻击力最低
        8:  生命最高
        9:  生命最低
        10: 随机1个,
        11: 随机2个,
        12: 随机4个,
        13: 随机x个后排
        14: 攻击力高->低排序
     */
        static findTargets(caster: Role, type: number, findEnemys: boolean = true): Array<Role> {
            var list: Array<Role> = [];
            switch (type) {
                case 1:
                    list = this.normal(caster, findEnemys);
                    break;
                case 2:
                    list = this.front(caster, findEnemys);
                    break;
                case 3:
                    list = this.background(caster, findEnemys);
                    break;
                case 4:
                    list = this.random(caster, 3, findEnemys);
                    break;
                case 5:
                    list = this.all(caster, findEnemys);
                    break;
                case 6:
                    list = this.maxAtk(caster, findEnemys);
                    break;
                case 7:
                    list = this.minAtk(caster, findEnemys);
                    break;
                case 8:
                    list = this.maxHp(caster, findEnemys);
                    break;
                case 9:
                    list = this.minHp(caster, findEnemys);
                    break;
                case 10:
                    list = this.random(caster, 1, findEnemys);
                    break;
                case 11:
                    list = this.random(caster, 2, findEnemys);
                    break;
                case 12:
                    list = this.random(caster, 4, findEnemys);
                    break;
                case 13:
                    list = this.randomBackground(caster, 2, findEnemys);
                    break;
                case 14:
                    list = this.maxToMinAtk(caster, findEnemys);
                    break;
                default:
                    break;
            }
            return list;
        }
    
        /** 按阵容位置优先级找1个*/
        static normal(caster: Role, findEnemys: boolean): Array<Role> {
            let r: Array<Role> = []!;
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            
            list.sort((role1, role2) => {
                return role1.pos - role2.pos;
            })
    
            if (list.length) r = [list[0]];
    
            return r;
        }
    
        /** 攻击力高->低*/
        static maxToMinAtk(caster: Role, findEnemys: boolean): Array<Role> {
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            list.sort((role1, role2) => {
                return role2.atk - role1.atk;
            });
    
            return list;
        }
    
        /** 攻击力最高*/
        static maxAtk(caster: Role, findEnemys: boolean): Array<Role> {
            let r: Role = null!;
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            list.forEach(element => {
                if (r) {
                    if (element.atk > r.atk) {
                        r = element;
                    }
                } else {
                    r = element;
                }
            });
    
            return [r];
        }
    
        /**攻击力最低 */
        static minAtk(caster: Role, findEnemys: boolean): Array<Role> {
            let r: Role = null!;
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            list.forEach(element => {
                if (r) {
                    if (element.atk < r.atk) {
                        r = element;
                    }
                } else {
                    r = element;
                }
            });
    
            return [r];
        }
    
        /** 生命最高*/
        static maxHp(caster: Role, findEnemys: boolean): Array<Role> {
            let r: Role = null!;
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            list.forEach(element => {
                if (r) {
                    if (element.getNumericValue(RoleAttributeType.hp_battle) > r.getNumericValue(RoleAttributeType.hp_battle)) {
                        r = element;
                    }
                } else {
                    r = element;
                }
            });
    
            return [r];
        }
    
        /**生命最低 */
        static minHp(caster: Role, findEnemys: boolean): Array<Role> {
            let r: Role = null!;
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            list.forEach(element => {
                if (r) {
                    if (element.getNumericValue(RoleAttributeType.hp_battle) < r.getNumericValue(RoleAttributeType.hp_battle)) {
                        r = element;
                    }
                } else {
                    r = element;
                }
            });
    
            return [r];
        }
    
        /** 打全体*/
        static all(caster: Role, findEnemys: boolean): Array<Role> {
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
    
            return list;
        }
    
        /**
         * 随机x个
         * @param caster 攻击者
         * @param num 随机数量
         * @returns 
         */
        static random(caster: Role, num: number, findEnemys: boolean): Array<Role> {
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            let randomList: Array<Role> = []!;
            list.forEach(element => {
                randomList.push(element);
            });
    
            var l = RandomManager.instance.getRandomByObjectList(randomList, num);
            return l;
        }
    
        /**
         * 随机x个后排
         * @param caster 攻击者
         * @param num 随机数量
         * @returns 
         */
        static randomBackground(caster: Role, num: number, findEnemys: boolean) {
            let backgroundList: Array<Role> = this.background(caster, findEnemys);
            var l = RandomManager.instance.getRandomByObjectList(backgroundList, num);
    
            return l;
        }
    
        /**
         * 打前排
         * @param caster 攻击者
         * @returns 
         */
        static front(caster: Role, findEnemys: boolean): Array<Role> {
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            let frontList: Array<Role> = []!;
            list.forEach(element => {
                if (element.pos == 1 || element.pos == 2 || element.pos == 6) {
                    frontList.push(element);
                }
            });
    
            if (frontList.length == 0) {
                frontList = this.background(caster, findEnemys);
            }
    
            return frontList;
        }
    
        /**
         * 打后排
         * @param caster 攻击者
         * @returns 
         */
        static background(caster: Role, findEnemys: boolean): Array<Role> {
            let list: Array<Role> = findEnemys ? RoleSelector.difangRoles(caster) : RoleSelector.youfangRoles(caster);
            let backgroundList: Array<Role> = []!;
            list.forEach(element => {
                if (element.pos == 3 || element.pos == 4 || element.pos == 5 || element.pos == 6) {
                    backgroundList.push(element);
                }
            });
    
            if (backgroundList.length == 0) {
                backgroundList = this.front(caster, findEnemys);
            }
    
            return backgroundList;
        }
    
        /** 点击屏幕随机一个怪物*/
        static randomFire(): Role {
            let list: Array<Role> = []!;
            smc.scene.MapModelRole.enemys.forEach(element => {
                if (element.getNumericValue(RoleAttributeType.hp_battle) > 0) {
                    list.push(element);
                }
            });
            return RandomManager.instance.getRandomByObjectList(list, 1)[0];
        }
}